import { IResponse } from '../interface';

export class BaseController {
  protected ERROR_CODE_DEFAULT = -1;
  protected ERROR_CODE_LOGIN = 40001;
  protected ERROR_CODE_TOKEN_EMPTY = 40002;
  protected ERROR_CODE_TOKEN_ERROR = 40003;

  protected ERROR_MESSAGE_MAP = {
    [this.ERROR_CODE_DEFAULT]: '请求失败',
    [this.ERROR_CODE_LOGIN]: '用户名或密码错误',
    [this.ERROR_CODE_TOKEN_EMPTY]: 'token emptry',
    [this.ERROR_CODE_LOGIN]: 'token error',
  };

  successResponse<T>(data: T = null): IResponse<T> {
    return {
      data: data,
      message: 'success',
      code: 0,
    };
  }
  errorResponse(code: number = -1) {
    return {
      data: null,
      message: this.ERROR_MESSAGE_MAP[code],
      code: code,
    };
  }
}
